package com.thirteenClassicsServer.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.thirteenClassics.dto.CommonDTO;
import com.thirteenClassics.dto.PictureCatalogQueryDTO;
import com.thirteenClassics.dto.PictureDTO;
import com.thirteenClassics.entity.Book;
import com.thirteenClassics.entity.Catalog;
import com.thirteenClassics.vo.CatalogVO;
import com.thirteenClassicsServer.mapper.CatalogBookMapper;
import com.thirteenClassicsServer.mapper.PictureMapper;
import com.thirteenClassicsServer.service.IPictureService;

@Service
public class PictureServiceImpl implements IPictureService {
    @Resource
    private PictureMapper pictureMapper;

    @Resource
    private CatalogBookMapper catalogBookMapper;

    @Override
    public void savePicture(Book book) {
        //        List<String> pictures=book.getPictures();
        //        PictureDto pictureData = new PictureDto("Sample Book", "https://example.com/pic1.jpg",
        //        LocalDateTime.now());
        //
        //        for(String picture : pictures){
        //            pictureMapper.insert()
        //        }

    }

    @Override
    public CatalogVO selectName(PictureCatalogQueryDTO pictureCatalogQueryDTO) {
        List<Catalog> list = pictureMapper.selectCatalogOrdersByName(pictureCatalogQueryDTO);
        CatalogVO catalogVO = new CatalogVO();
        catalogVO.setId(pictureCatalogQueryDTO.getIndex());
        catalogVO.setName(list.get(pictureCatalogQueryDTO.getIndex().intValue() - 1).getContents());
        catalogVO.setTot(Long.valueOf(list.size()));
        catalogVO.setCatalogId(list.get(pictureCatalogQueryDTO.getIndex().intValue() - 1).getId());
        return catalogVO;
    }

    @Override
    public List<Catalog> selectCatalogList(PictureCatalogQueryDTO pictureCatalogQueryDTO) {
        List<Catalog> list = pictureMapper.selectMinorityCatalogOrdersByName(pictureCatalogQueryDTO);
        return list;
    }

    @Override
    public List<Catalog> selectCatalogListJdb(PictureCatalogQueryDTO pictureCatalogQueryDTO) {
        List<Catalog> list = pictureMapper.selectCatalogOrdersByName(pictureCatalogQueryDTO);
        return list;
    }


    @Override
    public void importPicInsert(CommonDTO commonDTO) {

        //2、得到章节录入主键id，查询出来
        Long id = catalogBookMapper.selectCatalogBookId(commonDTO);
        //System.out.println(id);
        //3、插入图片章节表
        List<String> pic = commonDTO.getPictureUrl();


        for (int i = 0; i < pic.size(); i++) {
            PictureDTO pictureDTO = new PictureDTO();
            pictureDTO.setPictureUrl(pic.get(i));
            pictureDTO.setEntryId(id);

            String numberPart = pic.get(i).substring(pic.get(i).length() - 7, pic.get(i).length() - 4);

            // 解析为Long类型
            Long parsedNumber = Long.parseLong(numberPart);

            pictureDTO.setOrder(parsedNumber);
            pictureMapper.insert(pictureDTO);
        }
    }
}
